package com.inventory.spring.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.inventory.form.CorporationForm;
import com.inventory.persistence.Corporation;
import com.inventory.persistence.Country;
import com.inventory.persistence.State;
import com.inventory.persistence.User;
import com.inventory.service.AddressService;
import com.inventory.service.CorporationService;
import com.inventory.service.UserService;

@Controller
public class EnrollmentController {
	@Autowired
	private CorporationService	corporationService;
	@Autowired
	private AddressService		addressService;
	@Autowired
	private UserService			userService;
	@Autowired
	private Validator			companyValidator;
	@Autowired
	private Validator			userValidator;
	
	private final static String	BACK_URL	= "login.do";
	
	@RequestMapping(method = RequestMethod.GET, value = "/registerUser.do")
	public String registerUser(HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("userForm", new User());
		setupCorporations(model);
		model.addAttribute("backUrl", BACK_URL);
		return "userRegister";
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/registerCorporation.do")
	public String registerCorporation(HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("corporationForm", new CorporationForm());
		setupStatesAndCoutry(model);
		model.addAttribute("backUrl", BACK_URL);
		return "companyRegister";
	}
	
	@RequestMapping(value = "/createCorporation.do", method = RequestMethod.POST)
	public String submitCorporation(@ModelAttribute("corporationForm") CorporationForm corporationForm, HttpServletRequest request, BindingResult result, Model model) {
		companyValidator.validate(corporationForm, result);
		if (result.hasErrors()) {
			setupStatesAndCoutry(model);
			model.addAttribute("backUrl", BACK_URL);
			return "companyRegister";
		}
		Corporation corporation = corporationForm.getCorporation();
		corporationService.createCorporation(corporation);
		return "redirect:" + BACK_URL;
	}
	
	@RequestMapping(value = "/createUser.do", method = RequestMethod.POST)
	public String submitUser(@ModelAttribute("userForm") User user, HttpServletRequest request, BindingResult result, Model model) {
		userValidator.validate(user, result);
		if (result.hasErrors()) {
			setupCorporations(model);
			model.addAttribute("backUrl", BACK_URL);
			return "userRegister";
		}
		userService.createUser(user);
		return "redirect:" + BACK_URL;
	}
	
	private void setupStatesAndCoutry(Model model) {
		List<Country> country = addressService.findAllCountries();
		List<State> states = addressService.getStatesByCountry(country.get(0));
		model.addAttribute("countries", country);
		model.addAttribute("states", states);
	}
	
	private void setupCorporations(Model model) {
		List<Corporation> list = corporationService.getAllCorporations();
		model.addAttribute("firms", list);
	}
	
	public void setCorporationService(CorporationService corporationService) {
		this.corporationService = corporationService;
	}
	
	public void setAddressService(AddressService addressService) {
		this.addressService = addressService;
	}
	
	public void setCompanyValidator(Validator companyValidator) {
		this.companyValidator = companyValidator;
	}
	
	public void setUserValidator(Validator userValidator) {
		this.userValidator = userValidator;
	}
	
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
}
